package inf.coach.mobile;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class RateOrientation extends Activity{
	
	 public static long userid = 0;
	 public static long osmid= 0;
	 public RadioGroup mRadioGroup; 
	 public Button send;
	 public int rate=1;
	 
		
		public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.ratingorientation);
	        setTitle("Orientierung");
	        
	       	Bundle bundle = this.getIntent().getExtras();      
			userid = bundle.getLong("userid");
			osmid= bundle.getLong("osmid");
			
			send = (Button) findViewById(R.id.button_Orientation_Absenden);
			
			mRadioGroup= (RadioGroup)  findViewById(R.id.radioGroup_Orientation);
			
			mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
				
				   public void onCheckedChanged(RadioGroup group, int checkedId)
				    { 
					   if(R.id.radio0_orientation == checkedId)
				          {
				        	  rate=1;
				          }
				          if(R.id.radio1_orientation == checkedId)
				          {
				        	  rate=2;
				          }
				          if(R.id.radio2_orientation == checkedId)
				          {
				        	  rate=3;
				          }
				          if(R.id.radio3_orientation == checkedId)
				          {
				        	  rate=4;
				          }
				          if(R.id.radio4_orientation == checkedId)
				          {
				        	  rate=5;
				          }
				    }
				
			});
			
			send.setOnClickListener(new Button.OnClickListener(){
				public void onClick(View arg0){
					
					if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
    				{
    									
    				String text = webservice.createAssessment(userid, osmid, 0,0,rate);
    				System.out.println(text);
    				if (!text.equals(null) && text.equals("successful")) {
    					dialog();

    				}
    				if (text.equals(null) || text.equals("failed")) {
    					message.show(RateOrientation.this,"Fehlgeschlagen", "Bitte überprüfen Internetverbindung!");
    				}
    				}
            			else{
        					message.show(RateOrientation.this,"Fehler", "Keine Internetverbindung");
        				}
				}
				
			});
			
			
		}
		
		protected void dialog() { 
	        AlertDialog.Builder builder = new Builder(RateOrientation.this); 
	        builder.setMessage("Automatisch zur letzten Seite zurück!"); 
	        builder.setTitle("Erfolg"); 
	        builder.setPositiveButton("ok", 
	                new android.content.DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                        dialog.dismiss(); 
	                        RateOrientation.this.finish();
	                    }

	                }); 
	        
	        builder.create().show(); 
	    } 
		 protected void dialoggoback() { 
		        AlertDialog.Builder builder = new Builder(RateOrientation.this); 
		        builder.setMessage("Möchten Sie zur letzten Seite zurück springen?"); 
		        builder.setTitle("Information"); 
		        builder.setPositiveButton("ok", 
		                new android.content.DialogInterface.OnClickListener() { 
		                    @Override 
		                    public void onClick(DialogInterface dialog, int which) { 
		                        dialog.dismiss(); 
		                        RateOrientation.this.finish(); 
		                    }

		                }); 
		        builder.setNegativeButton("abbrechen", 
		                new android.content.DialogInterface.OnClickListener() { 
		                    @Override 
		                    public void onClick(DialogInterface dialog, int which) { 
		                        dialog.dismiss(); 
		                    } 
		                }); 
		        builder.create().show(); 
		    } 
		    @Override 
		    public boolean onKeyDown(int keyCode, KeyEvent event) { 
		        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
		            dialoggoback(); 
		            return false; 
		        } 
		        return false; 
		    } 

}